package cn.lingyangwl.agile.auth.controller;

import cn.lingyangwl.agile.auth.model.rqrs.CaptchaCheckReq;
import cn.lingyangwl.agile.auth.model.rqrs.CaptchaResp;
import cn.lingyangwl.agile.auth.service.CaptchaService;
import cn.lingyangwl.agile.common.security.annotation.Anonymous;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.security.ratelimit.LimitTypeEnum;
import cn.lingyangwl.framework.security.ratelimit.RateLimit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 验证码
 * @author shenguangyang
 */
@Slf4j
@RestController
@RequestMapping("/captcha")
public class CaptchaController {
    @Resource
    private CaptchaService captchaService;

    @GetMapping("/image")
    @RateLimit(key = "rate_limit::captcha", time = 20, count = 10, limitType = LimitTypeEnum.IP,
            msg = "操作过于频繁, 请 ${remainingTime} 后再次尝试")
    @Anonymous
    public Resp<CaptchaResp> createCaptcha() {
        return captchaService.createCaptcha();
    }

    @PostMapping("/check")
    public Resp<Boolean> checkCaptcha(@Validated @RequestBody CaptchaCheckReq req) {
        return Resp.ok(captchaService.checkCaptcha(req.getImageCaptcha(), req.getUuid()));
    }

    @PostMapping("/delete")
    public Resp<?> deleteCaptcha(@RequestParam("uuid") String uuid) {
        captchaService.deleteCaptcha(uuid);
        return Resp.ok();
    }
}
